package com.hbusa.cotroller.index;

import com.hbusa.dto.NovelMultiple;
import com.hbusa.dto.TbCategory;
import com.hbusa.dto.TbNovels;
import com.hbusa.service.service.impl.CategoryServiceImpl;
import com.hbusa.service.service.impl.NovelsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/index1")
public class IndexServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NovelsServiceImpl novelsService = new NovelsServiceImpl();
        List<TbNovels> tbNovels = novelsService.findAll();
        HttpSession session = req.getSession();
        session.setAttribute("tbNovels",tbNovels);
        // 图片 标签
        ArrayList<NovelMultiple> novelMultiples = new ArrayList<>();
        CategoryServiceImpl categoryService = new CategoryServiceImpl();
        for (TbNovels tbNovel : tbNovels) {
            NovelMultiple novelMultiple = new NovelMultiple();
            String classifyId = tbNovel.getClassifyId();
//            System.out.println(Long.parseLong(classifyId));
            TbCategory tbCategory = categoryService.doFindById(Long.parseLong(classifyId));
            novelMultiple.setNovel(tbNovel);
            novelMultiple.setCategory(tbCategory);
            novelMultiples.add(novelMultiple);
        }
        System.out.println(novelMultiples);
        session.setAttribute("novelMultiples", novelMultiples);

        resp.sendRedirect("/index.jsp");
    }
}
